import java.io.*;
import java.util.*;

public class Board {
	
	private Block bl[];
	private Player p[];
	private Scanner s;//normal reading
	private Scanner load;
	private int playerQueue = 0, propertyCount, utilityCount, railRoadCount;
	private boolean isGodMode;
	
	
	public Board(Player play[]){
		
		boolean triggerLoad = false;
		String loadPath = null;
		
		this.bl= new Block[41];
		this.p=play;
		
		if(this.p.length != 1)
			this.isGodMode=false;
		else{
			
			if(this.p[0].getPlayerName() == "Tester"){
				
				this.isGodMode=true;
				
			}else{
				
				triggerLoad = true;
				loadPath = this.p[0].getPlayerName();
				
			}
				
		}
		
		this.openFile();
		this.readFile();
		this.closeFile();
		
		
		this.countBuyableBlocks();
		
		if(triggerLoad){
			
			load(loadPath);
			
		}
		
		
	}
	
	public Player[] getP(){
		return p;
	}
	
	public boolean getIsGodMode(){
		return isGodMode;
	}
	public int getPlayerQueue(){
		return playerQueue;
	}
	  
	public Player getCurrentPlayer(){
		return p[this.getPlayerQueue()];
	}

	public Block getOtherBl(int num){
		return bl[num];
	}
	
	public Block[] getBlkArray(){
		return bl;
	}
	
	public int getPropertyCount(){
		return propertyCount;
	}
	
	public int getUtilityCount(){
		return utilityCount;
	}
	
	public int getRailRoadCount(){
		return railRoadCount;
	}
	
	public void setPropertyCount(int propCount){
		this.propertyCount = propCount;
	}
	
	public void setUtilityCount(int utilCount){
		this.utilityCount = utilCount;
	}
	
	public void setRailRoadCount(int railCount){
		this.railRoadCount = railCount;
	}
	
	public void passGo(){
		p[playerQueue].addMoney(200);
	}
	
	public void setPlayerQueueZero(){
		this.playerQueue = 0;
	}
	
	public void setPlayerQueuePlusOne(){
		this.playerQueue=playerQueue+1;
	}
	
	
	/****THIS IS FOR READING THE TEXT FILE******/
	
	public void readFile(){
		int counter = 0;
		do{
			if(counter==0){
				this.bl[counter] = new Go(s.next(), counter);
			}else if(counter == 2 || counter == 17 || counter == 33){
				this.bl[counter] = new CommunityChest(s.next(), counter);
			}else if(counter == 4 || counter == 38){
				this.bl[counter] = new Tax(s.next(), Integer.parseInt(s.next()), counter, s.next());
			}else if(counter == 5 || counter == 15 || counter == 25 || counter == 35){
				this.bl[counter] = new RailRoad(s.next(), Integer.parseInt(s.next()), s.next(), s.next(), s.next(), s.next(), counter);
			}else if(counter == 7 || counter == 22 || counter == 36){
				this.bl[counter] = new Chance(s.next(),counter);
			}else if(counter == 10){
				this.bl[counter] = new JustVisiting(s.next(),counter);			
			}else if(counter == 12 || counter == 28){
				this.bl[counter] = new Utilities(s.next(),Integer.parseInt(s.next()), s.next(), s.next(), s.next(), s.next(), counter);
			}else if(counter == 20){
				this.bl[counter] = new FreeParking(s.next(),counter);
			}else if(counter == 30){
				this.bl[counter] = new Parak(s.next(), counter);
			}else if(counter == 40){
				this.bl[counter] = new InsideJail(s.next(),counter);
			}else{
				this.bl[counter] = new PropertyBlock(
						s.next(),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Integer.parseInt(s.next()),
						Boolean.parseBoolean(s.next()),
						s.next(),
						s.next(),
						s.next(),
						s.next(),
						counter);
			}
			counter++;
		}while(s.hasNext());
	}
	
	public void loadSavedGame(){
		
		int playerCounter = 0;
		String flag;
		do{
			flag = load.next();
			if(flag.equals("NumberOfPlayers")){
				
				this.p = new Player[Integer.parseInt(load.next())];
				
			}else if(flag.equals("\nPlayer")){
				
				this.p[playerCounter] = new Player("loaded");
				this.p[playerCounter].setPlayerName(load.next());
				this.p[playerCounter].setCurrentMoney(Integer.parseInt(load.next()));
				this.p[playerCounter].setLastRoll(Integer.parseInt(load.next()));
				this.p[playerCounter].setDoubleRollCount(Integer.parseInt(load.next()));
				this.p[playerCounter].setChanceJailToken(Integer.parseInt(load.next()));
				this.p[playerCounter].setCommunityJailToken(Integer.parseInt(load.next()));
				this.p[playerCounter].setHighRoll(Integer.parseInt(load.next()));
				this.p[playerCounter].setQueueSpot(Integer.parseInt(load.next()));
				this.p[playerCounter].setComponentPointer(Integer.parseInt(load.next()));
				this.p[playerCounter].setJailCounter(Integer.parseInt(load.next()));
				
				//block related variables
				this.p[playerCounter].setCurrentBlockPos(this.bl[Integer.parseInt(load.next())]);
				this.p[playerCounter].setNextBlockPos(this.bl[Integer.parseInt(load.next())]);
				
				//active
				if(load.next().equals("true")){
					
					this.p[playerCounter].setActive(true);
					
				}else{
					
					this.p[playerCounter].setActive(false);
					
				}
				
				this.p[playerCounter].setPlayerToken(load.next());
				playerCounter++;
			}else if(flag.equals("\nElementOfCurrentPlayer")){
				
				this.playerQueue = Integer.parseInt(load.next());
				
			}else if(flag.equals("\nBlock")){
					String type = load.next();
					if(type.equals("RailRoad")){
						
						int blockNumber  = Integer.parseInt(load.next());
						
						//setOwner
						String holder= load.next();
						if(holder.equals("null")){
							((RailRoad) this.bl[blockNumber]).setCOwner(null);
						}else{
							((RailRoad) this.bl[blockNumber]).setCOwner(this.p[Integer.parseInt(holder)]);
						}
						
						((RailRoad) this.bl[blockNumber]).setIsMortgaged(load.nextBoolean());
						
					}else if(type.equals("Utilities")){
						
						int blockNumber  = Integer.parseInt(load.next());
						
						//setOwner
						String holder= load.next();
						if(holder.equals("null")){
							((Utilities) this.bl[blockNumber]).setCOwner(null);
						}else{
							((Utilities) this.bl[blockNumber]).setCOwner(this.p[Integer.parseInt(holder)]);
						}
						
						((Utilities) this.bl[blockNumber]).setIsMortgaged(load.nextBoolean());
						
					}else if(type.equals("PropertyBlock")){
						
						int blockNumber  = Integer.parseInt(load.next());
						
						((PropertyBlock) this.bl[blockNumber]).setCurrentNumberOfHouses(Integer.parseInt(load.next()));
						
						String holder= load.next();
						if(holder.equals("null")){
							((PropertyBlock) this.bl[blockNumber]).setCOwner(null);
						}else{
							((PropertyBlock) this.bl[blockNumber]).setCOwner(this.p[Integer.parseInt(holder)]);
						}
						
						((PropertyBlock) this.bl[blockNumber]).setIsMortgaged(load.nextBoolean());
						
						
					}
				
			}
			
		}while(load.hasNext());
		
	}
	
	public void openFile(){
		try{
			this.s = new Scanner(new File("src/block.txt"));
			this.s.useDelimiter(",");
		}catch(Exception e){
			System.out.println("Error Reading File");
		}
	}
	
	public void openLoad(String path){
		try{
			this.load = new Scanner(new File("SaveGame/"+path));
			this.load.useDelimiter("<>");
		}catch(Exception e){
			System.out.println("Error Reading File");
		}
	}
	
	public void closeFile(){
		this.s.close();
	}
	public void closeLoad(){
		this.load.close();
	}
	
	public void countBuyableBlocks(){
		int tempPropCount = 0;
		int tempUtilCount = 0;
		int tempRailCount = 0;
		
		for(int i = 0; i<this.bl.length; i++){
			if(this.bl[i] instanceof PropertyBlock){
				tempPropCount++;
			}else if(this.bl[i] instanceof Utilities){
				tempUtilCount++;
			}else if(this.bl[i] instanceof RailRoad){
				tempRailCount++;
			}
			
			setPropertyCount(tempPropCount);
			setUtilityCount(tempUtilCount);
			setRailRoadCount(tempRailCount);
		}
		
		System.out.println(getRailRoadCount());
		System.out.println(getUtilityCount());
		System.out.println(getPropertyCount());
		
	}
	
	
	
	public void load(String path){
		
		this.openLoad(path);
		this.loadSavedGame();
		this.closeLoad();
		
		
	}
}
